<?php
require_once ('model/BaseModel.class.php');
class EjemploModel extends BaseModel {
	public function getIndex() {
		$texto = "No hay acciones";
		return $texto;
	}
	public function getAdd() {
		$nombre = isset ( $_POST ['nombre'] ) ? $_POST ['nombre'] : "";
		$apellido = isset ( $_POST ['apellido'] ) ? $_POST ['apellido'] : "";
		$telefono = isset ( $_POST ['telefono'] ) ? $_POST ['telefono'] : "";
		$sexo = isset ( $_POST ['sexo'] ) ? $_POST ['sexo'] : "";
		
		$s = <<<SQL
insert into empleados(nombre,apellido,telefono,sexo)
values (:nombre, :apellido, :telefono, :sexo)
SQL;
		$sp = $this->db->prepare ( $s );
		$sp->bindParam ( ':nombre', $nombre );
		$sp->bindParam ( ':apellido', $apellido );
		$sp->bindParam ( ':telefono', $telefono );
		$sp->bindParam ( ':sexo', $sexo );
		$sp->execute ();
	}
	public function listar() {
		
		return $this->generarTabla();
		
	}
	public function getColunns() {
		
		$cons = "select column_name from columns where table_schema = 'php' and table_name='empleados'";
		$res = $this->db->query ( $cons );
		
		if (! $res) {
			return null;
		} else {
			$sol = array ();
			foreach ( $res as $fila ) {
				$i = 0;
				foreach ( $fila as $indice => $valor ) {
					if ($i % 2 == 0) {
						array_push ( $sol, $valor );
					}
					$i ++;
				}
			}
			return $sol;
		}
	}
	public function generarTabla() {
		$colum = $this->getColunns();
		
		$consulta = "select * from empleados";
		$resultado = $this->db->query ( $consulta );
		
		$tabla = "";
		
		foreach ( $resultado as $fila ) {
			$i = 0;
			$tabla .= "<tr>\r\n";
			foreach ( $fila as $valores ) {
				if ($i % 2 == 0) {
					$tabla .= "<td>$valores</td>\r\n";
				}
				$i ++;
			}
			$tabla .= "</tr>\r\n";
		}
		
		return "<table> $tabla </table>";
	}
}